package svgedit.gui.command;

import svgedit.gui.View;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGPaint;
import svgedit.svg.SVGStylable;

/**
 *  Define change fill execution as a command object which has 
 *      execute (redo) and undo methods
 * */
public class FillChangeCommand implements Command
{
	private View view;
	private SVGPaint paint;
	private SVGPaint backupPaint;
	private SVGPaint changePaint;
	private SVGElement backupElem;
	private SVGElement element;
 	
	/** Constructor for fill change command object
	 * 
	 * @param view to get the view panel
	 * @param paint to get the new paint for the item
	 * */
	public FillChangeCommand(View view,SVGElement element,SVGPaint paint ){
		this.view = view;
        this.element = element;
		this.paint = paint;
		backupPaint = new SVGPaint();
		changePaint = new SVGPaint();
		backupElem = null;
	}
	
	/**
	 *  Change items fill when execute
	 * */
	public void execute(){
		if (backupElem == null){
			backupPaint.setRGBColor(((SVGStylable) element).getFill().getRGBColor());
			changePaint.setRGBColor(paint.getRGBColor());
			backupElem = element;
			((SVGStylable) element).getFill().setValueFromPaint(paint);
		}else {
			((SVGStylable) backupElem).getFill().setValueFromPaint(changePaint);
			view.repaint();
		}
		
	}
	
	/**
	 *  Change items fill color back using backupPaint
	 * */
	public void undo(){
		((SVGStylable) backupElem).getFill().setValueFromPaint(backupPaint);
		view.repaint();
	}
     
}
